package com.xsprite.managesys.servlet.playerlog;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xsprite.managesys.bean.LogParameter;
import com.xsprite.managesys.bean.PlayerInfoLog;
import com.xsprite.managesys.bean.PlayerInfoLogPojo;
import com.xsprite.managesys.bean.QueryServer;
import com.xsprite.managesys.service.LogAnalyzer;
import com.xsprite.managesys.service.ServersDatabaseManager;
import com.xsprite.managesys.servlet.BaseServlet;

/**
 * Servlet implementation class QueryPlayerLogServlet
 */
public class QueryPlayerLogServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private final long ONEDAY = 1000 * 60 * 60 * 24;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		super.service(req, resp);
		String userid = req.getSession().getAttribute("userid").toString();
		try{
			if(isParameterNull("playerName") || isParameterNull("serverId") || isParameterNull("date")){
				return;
			}
			String playerName = queryParam("playerName");
			String serverId = queryParam("serverId");
			String date = queryParam("date");
			String toQueryTable = getQueryTable(date);
			long[] period = getPeriod(date);
			QueryServer server = ServersDatabaseManager.getInstance().getServerById(Integer.parseInt(serverId.split("_")[0]));
			PlayerInfoLog log = new PlayerInfoLog();
			log.setPlayerName(playerName);
			log.setToQueryTable(toQueryTable);
			log.setStartTime(period[0]);
			log.setEndTime(period[1]);
			List<PlayerInfoLog> playerInfoLogs = ServersDatabaseManager.getInstance().queryPlayerInfoLog(server.getGameDBId(), server.getLogDBId(), userid, log);
			List<PlayerInfoLogPojo> logs = getPlayerInfoLogPojos(playerInfoLogs);
			req.setAttribute("logs", logs);
			ServersDatabaseManager manager = ServersDatabaseManager.getInstance();
			List<QueryServer> servers = manager.getServersLsit();
			req.setAttribute("servers", servers);
			req.setAttribute("playerName", playerName);
			req.setAttribute("serverId", serverId);
			forwardTo("/view/playerlog/queryPlayerLog.jsp");
		} catch (Exception ex){
			ex.printStackTrace();
		}
	}
	
	private List<PlayerInfoLogPojo> getPlayerInfoLogPojos(List<PlayerInfoLog> logs){
		List<PlayerInfoLogPojo> pojos = new ArrayList<PlayerInfoLogPojo>();
		for(PlayerInfoLog pil : logs){
			PlayerInfoLogPojo pojo = new PlayerInfoLogPojo();
			pojo.setContent(getPojoContent(pil));
			pojos.add(pojo);
		}
		return pojos;
	}
	
	private String getPojoContent(PlayerInfoLog log){
		StringBuffer sb = new StringBuffer();
		if(LogParameter.getLogParameterById(log.getLogParameter()) == null){
			return "can't resolve logParameter : " + log.getLogParameter();
		}
		sb.append(log.getPlayerName() + "在" + parseTime(log.getCreateTime()) 
					+ "通过[" + LogParameter.getLogParameterById(log.getLogParameter()).getName() + "]操作," + LogAnalyzer.analyze(log.getInfo()));
		return sb.toString();
	}
	
	private String parseTime(long time){
		SimpleDateFormat myFmt=new SimpleDateFormat("yy年MM月dd日 HH时mm分");
		return myFmt.format(new Date(time));
	}
	
	private String getQueryTable(String date) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
		Date d = sdf.parse(date);
		int year = d.getYear();
		int month = d.getMonth();
		int day = d.getDate();
		String toQueryTable = "PlayerInfoLog" + year % 100 + ((month > 9)?month:("0" + month));
		return toQueryTable;
	}
	
	private long[] getPeriod(String date) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
		Date d = sdf.parse(date);
		long[] period = new long[2];
		period[0] = d.getTime();
		period[1] = period[0] + ONEDAY;
		return period;
	}
}
